package ev;

import java.util.*;
import TeamPursuit.*;

public class EA_Team_Women1 implements Team_Women {
//easy version women

	@Override
	public void emulation() {

		//initial solution
		boolean [] strategy = {true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false};
	    int [] pacing = {400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400};
    	//pacing strategy must be in the range of 200-1200 W

	    //functional parameters
	    SimulationResult result = null;
        WomensTeamPursuit womensTeamPursuit = new WomensTeamPursuit();
        //simulation instance
        int times = 40000;//iteration

    	try {
    		result = womensTeamPursuit.simulate(strategy, pacing);
		} catch (Exception e) {
			e.printStackTrace();
		}//do emulation
        double finalTime = result.getFinishTime();//initial solution's final time

        System.out.println("The starting value is:");
        System.out.println(finalTime);
        System.out.println(Arrays.toString(strategy));
		System.out.println(Arrays.toString(pacing));//printout the initial result

        int index=0;
        int pValue=0;
        int PN=0;

        int failcount=0;
        int [] best_pacing=pacing.clone();//best backup
        double best_time=finalTime;//best backup

        int noreplace_count=300;
        double change=0.1;
        int parameter=2;
        Random r = new Random();
        boolean flag=true;

        //main loop
        while(times>0)
        {
        	int [] pacing2=pacing.clone();//must clone, or we'll be pointing to the same thing
        	flag=true;

        	if(noreplace_count==300)
			{
        		noreplace_count=0;
        		change=1/(double)parameter;//from 1/2 to 1/3, 1/4, 1/5....etc
				System.out.println("The new change parameter is "+change);
				parameter++;
			}

        	while(flag)//do mutate
        	{
				index = r.nextInt(pacing.length);
				//determine which one to mutate
				PN = r.nextInt(2);
				//if PN=0, negative; if =1, positive
				pValue = pacing[index];
				if(PN==0)
				{
					double pValue2 = pValue;
					pValue2 = pValue2-pValue2*change;
					pValue=(int) pValue2;
				}else
				{
					double pValue2 = pValue;
					pValue2 = pValue2+pValue2*change;
					pValue=(int) pValue2;
				}

				if(pValue>=200 && pValue<=1200)//a valid mutate
				{
					flag=false;
				}
        	}//end while

        	pacing2[index]=pValue;//do mutate on the copy

        	try {
        		result = womensTeamPursuit.simulate(strategy, pacing2);
			} catch (Exception e) {
				e.printStackTrace();
			}//do emulation

			if(result.getProportionCompleted() < 0.9999998){
				//this means the new pacing is an invalid one
				pacing2=null;
				failcount++;
			}else{
				//if the new pacing is valid
				if(result.getFinishTime()<finalTime){
					finalTime = result.getFinishTime();
					pacing=pacing2.clone();
					noreplace_count=0;
					System.out.println("at "+times+" hit!");
			        System.out.println(finalTime);
			        System.out.println(Arrays.toString(strategy));
					System.out.println(Arrays.toString(pacing));
				}
				else{
					pacing2=null;
					noreplace_count++;
				}
			}

			if(times%500==0)
			{System.out.println(times);}

			times--;
		}

		//loop end, print our result
        System.out.println(finalTime);
        System.out.println(Arrays.toString(strategy));
		System.out.println(Arrays.toString(pacing));
	}

}